<?php
define('HGB_API','http://192.168.1.199/hgb/user/api/dbapi.php');//担保交易地址
define('HGB_ID', '4');//接口id
define('HGB_KEY', 'qGx8g6NYwNNISVGxCRXNgsIpP95GsQjr');//接口密钥

define('HGB_RETURNSITEURL','http://192.168.1.199/weike/lz/');//接口回传地址，本地地址

/**
 * 对称加密
 * @param type $key 密钥
 * @param type $str 明文
 * @return string 密文
 */
function hgb_encode( $key, $str ){
        $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256,MCRYPT_MODE_ECB),MCRYPT_RAND);
        return mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $str, MCRYPT_MODE_ECB, $iv);
}

/**
 * 对称解密
 * @param type $key 解密
 * @param type $str 密文
 * @return string 明文
 */
function hgb_decode( $key, $str ){
        $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256,MCRYPT_MODE_ECB),MCRYPT_RAND);
        return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $str, MCRYPT_MODE_ECB, $iv);
}

/**
 * 与api交互
 * @param array $data 数据
 * @return string
 */
function _hgb_sendtoapi($data) {
    $data = array('id'=>HGB_ID, 'content'=>base64_encode(hgb_encode(HGB_KEY, json_encode($data))));
    $data=http_build_query($data);
    $opts = array(
        'http'=>array(
          'method'=>"POST",
          'header'=>"Content-type:application/x-www-form-urlencoded\r\nContent-Length:".strlen($data)."\r\n",
          'content'=> $data
        )
    );
    $context = stream_context_create($opts);
    return file_get_contents(HGB_API, FALSE, $context);
}

/**
 * 交易金额范围
 * @return array(min,max)
 */
function hgb_moneyrange() {
    $sendarr = array('op'=>'moneyrange');
    $rstr = _hgb_sendtoapi($sendarr);
    if ($rstr) {
        return json_decode(trim(hgb_decode(HGB_KEY, trim(base64_decode($rstr)))), TRUE);
    }
    return array();
}

/**
 * 查询余额和积分
 * @param int $uid 用户名
 * @return array(umoney,upoints)
 */
function hgb_getumoneyupoints($uid) {
    $sendarr = array('op'=>'getumoneyupoints','uid'=>$uid);
    $rstr = _hgb_sendtoapi($sendarr);
    if ($rstr) {
        return json_decode(trim(hgb_decode(HGB_KEY, trim(base64_decode($rstr)))), TRUE);
    }
    return array();
}

/**
 * 是否存在定单号
 * @param string $orderno 定单号
 * @return int
 */
function hgb_checkorderno($orderno) {
    $sendarr = array('op'=>'checkorderno','orderno'=>$orderno);
    $rstr = _hgb_sendtoapi($sendarr);
    if ($rstr) {
        return trim(hgb_decode(HGB_KEY, trim(base64_decode($rstr))));
    }
    return 0;
}

/**
 * 创建交易单
 * @param string $name 交易单名称
 * @param string $orderno 定单号
 * @param number $money 金额
 * @param int $uid 支出者id
 * @param int $touid 接收者id
 * @param string $touname 接收者称呼
 * @param string $vcode 识别码
 * @return array|int array(tid,tno,torderno,tstate) -1小于限制金额，-2大于限制金额，-3订单号已经存在，-4自己没有激活火工宝,-5商家没有激活火工宝,没有返回创建失败
 */
function hgb_creattrading($name, $orderno, $money, $uid, $touid, $touname, $vcode) {
    $sendarr = array('op'=>'creattrading','name'=>$name,'orderno'=>$orderno,'money'=>$money,'uid'=>$uid,'touid'=>$touid,'touname'=>$touname,'vcode'=>$vcode);
    $rstr = _hgb_sendtoapi($sendarr);
    if ($rstr) {
        return json_decode(trim(hgb_decode(HGB_KEY, trim(base64_decode($rstr)))), TRUE);
    }
    return array();
}

/**
 * 取消交易
 * @param array $no_and_vcode array('交易号'=>'验证码');
 */
function hgb_canceltrading($no_and_vcode) {
    if (is_array($no_and_vcode)) {
        $sendarr = array('op'=>'canceltrading','no_and_vcodes'=>json_encode($no_and_vcode));
        $rstr = _hgb_sendtoapi($sendarr);
    }
}

/**
 * 改价格
 * @param float $money
 * @param string $no
 * @param string $vcode
 * @return int 1成功，0失败，-1小于限额，-2大于限额
 */
function hgb_changemoney($money, $no, $vcode) {
    $sendarr = array('op'=>'changemoney','money'=>$money,'no'=>$no,'vcode'=>$vcode);
    $rstr = _hgb_sendtoapi($sendarr);
    if ($rstr) {
        return trim(hgb_decode(HGB_KEY, trim(base64_decode($rstr))));
    }
    return 0;
}

/**
 * 查询状态
 * @param string $orderno 定单号
 * @param string $vcode 识别码
 * @return int 0=>'进行中',1=>'未付款',2=>'等待发货',3=>'未确认收货',4=>'退款',5=>'成功',6=>'失败'
 */
function hgb_getstate($orderno, $vcode) {
    $sendarr = array('op'=>'getstate','orderno'=>$orderno,'vcode'=>$vcode);
    $rstr = _hgb_sendtoapi($sendarr);
    if ($rstr) {
        return trim(hgb_decode(HGB_KEY, trim(base64_decode($rstr))), TRUE);
    }
    return -1;
}

/**
 * 更新状态
 * @param int $state 0=>'进行中',1=>'未付款',2=>'等待发货',3=>'未确认收货',4=>'退款',5=>'成功',6=>'失败'
 * @param string $no 支付号
 * @param string $vcode 识别码
 * @return int 1成功
 */
function hgb_updatestate($state, $no, $vcode) {
    $sendarr = array('op'=>'updatestate','state'=>$state,'no'=>$no,'vcode'=>$vcode);
    $rstr = _hgb_sendtoapi($sendarr);
    if ($rstr) {
        return trim(hgb_decode(HGB_KEY, trim(base64_decode($rstr))));
    }
    return 0;
}

/**
 * 
 * @param type $money
 * @param type $no
 * @param type $vcode
 * @param type $pwd
 * @return int 1成功，0失败，-1交易单数据错误，-2用户无效，-3金额不足，-4扣款失败
 */
function hgb_dbpay($money, $no, $vcode, $pwd) {
    $sendarr = array('op'=>'dbpay','money'=>$money,'no'=>$no,'vcode'=>$vcode, 'pwd'=>$pwd);
    $rstr = _hgb_sendtoapi($sendarr);
    if ($rstr) {
        return trim(hgb_decode(HGB_KEY, trim(base64_decode($rstr))));
    }
    return 0;
}

/**
 * 退款退货
 * @param string $name 名称
 * @param string $orderno 定单号
 * @param float $money 金额
 * @param int $uid 商家用户id
 * @param string $uname 商家店名
 * @param string $touid 退款者
 * @return int -1订单号有误，-2退款金额合计大过订单金额，-3商家用户id无效，-4退款者无效，-5商家火工宝余额不足
 */
function hgb_refund($name, $orderno, $money, $uid, $uname, $touid) {
    $sendarr = array('op'=>'refund','name'=>$name,'orderno'=>$orderno,'money'=>$money,'uid'=>$uid,'uname'=>$uname, 'touid'=>$touid);
    $rstr = _hgb_sendtoapi($sendarr);
    if ($rstr) {
        return trim(hgb_decode(HGB_KEY, trim(base64_decode($rstr))));
    }
    return array();
}

/**
 * 测试本地到服务器的连接是否正确
 */
if (isset($_REQUEST['op']) && $_REQUEST['op'] == 'testconnettoserver') {
    $sendarr = array('op'=>'testconnettoserver','test'=>'test');
    echo trim(hgb_decode(HGB_KEY, trim(base64_decode(_hgb_sendtoapi($sendarr)))));
}

/**
 * 响应服务器到本地的连接测试
 */
if (isset($_REQUEST['id']) && $_REQUEST['id'] == HGB_ID && isset($_REQUEST['content'])) {
    $rarr = json_decode(trim(hgb_decode(HGB_KEY, trim(base64_decode($_REQUEST['content'])))), TRUE);
    if (isset($rarr['test']) && $rarr['test'] == 'test') {
        echo 'success';
    } else {
        echo 'error';
    }
}